package aliyun

import (
	"encoding/json"
	"errors"
	"fmt"
	"time"

	"github.com/aliyun/alibaba-cloud-sdk-go/sdk"
	"github.com/aliyun/alibaba-cloud-sdk-go/sdk/requests"
)

const (
	accessKeyId     = "LTAIyk37DT8mQUyY"
	accessKeySecret = "O6MC8Br7vC4uCUtMZ2n8sXEjDgaGqn"
)

var requestCount uint64 = uint64(time.Now().Unix())

//词性标注
func WordPos(sentence string) {

	client, err := sdk.NewClientWithAccessKey("cn-shanghai", accessKeyId, accessKeySecret)
	if err != nil {
		panic(err)
	}

	type args struct {
		Text string `json:"text"`
	}

	newArgs := args{

		Text: sentence,
	}
	reqArgs, _ := json.Marshal(newArgs)

	request := requests.NewCommonRequest()
	request.Domain = "nlp.cn-shanghai.aliyuncs.com"
	request.Version = "2018-04-08"

	// 因为是Restful接口，因此需指定PathPattern
	request.PathPattern = "/nlp/api/wordpos/general"
	request.Method = "POST"
	request.Headers["x-acs-signature-method"] = "HMAC-SHA1"
	request.Headers["x-acs-signature-nonce"] = fmt.Sprintf("%v", requestCount)
	request.SetContent(reqArgs)
	request.SetContentType("application/json;chrset=utf-8")
	response, err := client.ProcessCommonRequest(request)
	if err != nil {
		panic(err)
	}

	requestCount++

	fmt.Print(response.GetHttpContentString())
}

//情感分析
func EmotionAnalyse(sentence string) {

	client, err := sdk.NewClientWithAccessKey("cn-shanghai", accessKeyId, accessKeySecret)
	if err != nil {
		panic(err)
	}

	type args struct {
		Text string `json:"text"`
	}

	newArgs := args{

		Text: sentence,
	}
	reqArgs, _ := json.Marshal(newArgs)

	request := requests.NewCommonRequest()
	request.Domain = "nlp.cn-shanghai.aliyuncs.com"
	request.Version = "2018-04-08"

	// 因为是Restful接口，因此需指定PathPattern
	request.PathPattern = "/nlp/api/sentiment/ecommerce"
	request.Method = "POST"
	request.Headers["x-acs-signature-method"] = "HMAC-SHA1"
	request.Headers["x-acs-signature-nonce"] = fmt.Sprintf("%v", requestCount)
	request.SetContent(reqArgs)
	request.SetContentType("application/json;chrset=utf-8")
	response, err := client.ProcessCommonRequest(request)
	if err != nil {
		panic(err)
	}

	requestCount++

	fmt.Print(response.GetHttpContentString())
}

//命名实体 关键字段的含义
func GetEntity(sentence string) {

	client, err := sdk.NewClientWithAccessKey("cn-shanghai", accessKeyId, accessKeySecret)
	if err != nil {
		panic(err)
	}

	type args struct {
		Type string `json:"type"`
		Text string `json:"text"`
	}

	newArgs := args{
		Type: "full",
		Text: sentence,
	}
	reqArgs, _ := json.Marshal(newArgs)

	request := requests.NewCommonRequest()
	request.Domain = "nlp.cn-shanghai.aliyuncs.com"
	request.Version = "2018-04-08"

	// 因为是Restful接口，因此需指定PathPattern
	request.PathPattern = "/nlp/api/entity/ecommerce"
	request.Method = "POST"
	request.Headers["x-acs-signature-method"] = "HMAC-SHA1"
	request.Headers["x-acs-signature-nonce"] = fmt.Sprintf("%v", requestCount)
	request.SetContent(reqArgs)
	request.SetContentType("application/json;chrset=utf-8")
	response, err := client.ProcessCommonRequest(request)
	if err != nil {
		panic(err)
	}

	requestCount++

	fmt.Print(response.GetHttpContentString())
}

//中心词识别
func GetKeyOfSentence(sentence string) {

	client, err := sdk.NewClientWithAccessKey("cn-shanghai", accessKeyId, accessKeySecret)
	if err != nil {
		panic(err)
	}

	type args struct {
		Lan  string `json:"lang"`
		Text string `json:"text"`
	}

	newArgs := args{
		Lan:  "ZH",
		Text: sentence,
	}

	reqArgs, _ := json.Marshal(newArgs)

	request := requests.NewCommonRequest()
	request.Domain = "nlp.cn-shanghai.aliyuncs.com"
	request.Version = "2018-04-08"

	// 因为是Restful接口，因此需指定PathPattern
	request.PathPattern = "/nlp/api/kwe/ecommerce"
	request.Method = "POST"
	request.Headers["x-acs-signature-method"] = "HMAC-SHA1"
	request.Headers["x-acs-signature-nonce"] = fmt.Sprintf("%v", requestCount)
	request.SetContent(reqArgs)
	request.SetContentType("application/json;chrset=utf-8")
	response, err := client.ProcessCommonRequest(request)
	if err != nil {
		panic(err)
	}

	requestCount++

	fmt.Print(response.GetHttpContentString())
}

//分词
func WordSegment(sentence string) {

	client, err := sdk.NewClientWithAccessKey("cn-shanghai", accessKeyId, accessKeySecret)
	if err != nil {
		panic(err)
	}

	type args struct {
		Lan  string `json:"lang"`
		Text string `json:"text"`
	}

	newArgs := args{
		Lan:  "ZH",
		Text: sentence,
	}

	reqArgs, _ := json.Marshal(newArgs)

	request := requests.NewCommonRequest()
	request.Domain = "nlp.cn-shanghai.aliyuncs.com"
	request.Version = "2018-04-08"

	// 因为是Restful接口，因此需指定PathPattern
	request.PathPattern = "/nlp/api/wordsegment/general"
	request.Method = "POST"
	request.Headers["x-acs-signature-method"] = "HMAC-SHA1"
	request.Headers["x-acs-signature-nonce"] = fmt.Sprintf("%v", requestCount)
	request.SetContent(reqArgs)
	request.SetContentType("application/json;chrset=utf-8")
	response, err := client.ProcessCommonRequest(request)
	if err != nil {
		panic(err)
	}

	requestCount++

	fmt.Print(response.GetHttpContentString())
}

var tts *TokenManage
var gAppKey string

func InitTTS(appKey string) {

	gAppKey = appKey
	tts = newToken()
}

func NewTTS(audioSaveFile, text string) error {

	if tts == nil {
		return errors.New("tts not init")
	}

	return postTTS(gAppKey, tts.getToken(), text, audioSaveFile)
}
